package org.dslintegration.merge.wizard;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class MergeWizardPage extends WizardPage {
	private Composite container;

	private Text outputLocation;
	private String path;
	private Text outputName;
	private String name;

	private Button km3;
	private Button emf;
	private Button xmi;

	protected MergeWizardPage(String path) {
		super("Page");
		setTitle("Merge Metamodels");
		setDescription("Specify Merging Properties");
		this.path = path.substring(0, path.lastIndexOf("/"));
		this.name = path.substring(path.lastIndexOf("/")+1,path.length()-4);
		this.name = name.substring(0,1).toLowerCase()+name.substring(1);
	}

	@Override
	public void createControl(Composite parent) {
		container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 3;

		new Label(container, SWT.NULL).setText("Output Name");
		GridData oname = new GridData();
		oname.grabExcessHorizontalSpace = true;
		oname.horizontalSpan = 2;

		outputName = new Text(container, SWT.BORDER | SWT.SINGLE);
		outputName.setLayoutData(oname);
		outputName.setText(name);
		// create the desired layout for this wizard page

		Label outputFolder = new Label(container, SWT.NULL);
		outputFolder.setText("Output Location:");

		outputLocation = new Text(container, SWT.BORDER | SWT.SINGLE);
		outputLocation.setText(path);

		Button browse = new Button(container, SWT.PUSH);
		browse.setText("  Browse  ");

		Listener listener = new Listener() {
			public void handleEvent(Event event) {
				DirectoryDialog directoryDialog = new DirectoryDialog(new Shell());

				directoryDialog.setFilterPath(path);
				directoryDialog.setMessage("Please select a directory and click OK");

				String dir = directoryDialog.open();
				if (dir != null) {
					outputLocation.setText(dir);
				}

			}
		};

		browse.addListener(SWT.Selection, listener);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 1;
		outputLocation.setLayoutData(gd);

		// override = new Button(container, SWT.CHECK);
		// override.setSelection(false);
		// override.setText("Override");

		GridData gdoptions = new GridData(SWT.BEGINNING, SWT.TOP, false, false);
		gdoptions.grabExcessHorizontalSpace = true;
		gdoptions.horizontalAlignment = SWT.BEGINNING;
		gdoptions.horizontalIndent = 5;
		gdoptions.horizontalSpan = 3;

		Group options = new Group(container, SWT.None);
		options.setText("Export Formats");
		options.setLayoutData(gdoptions);

		GridLayout optionsLayout = new GridLayout(3, true);
		options.setLayout(optionsLayout);

		km3 = new Button(options, SWT.CHECK);
		km3.setText("KM3");
		km3.setSelection(true);

		xmi = new Button(options, SWT.CHECK);
		xmi.setText("XMI");
		xmi.setSelection(true);

		emf = new Button(options, SWT.CHECK);
		emf.setText("Ecore");
		emf.setSelection(true);

		container.pack();
		setControl(container);
		setPageComplete(true);
	}

	public Text getOutputLocation() {
		return outputLocation;
	}

	public void setOutputLocation(Text outputLocation) {
		this.outputLocation = outputLocation;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public Text getOutputName() {
		return outputName;
	}

	public void setOutputName(Text outputName) {
		this.outputName = outputName;
	}

	public Button getKm3() {
		return km3;
	}

	public void setKm3(Button km3) {
		this.km3 = km3;
	}

	public Button getEmf() {
		return emf;
	}

	public void setEmf(Button emf) {
		this.emf = emf;
	}

	public Button getXmi() {
		return xmi;
	}

	public void setXmi(Button xmi) {
		this.xmi = xmi;
	}

}
